package com.kaka.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kaka.pojo.Product;
import com.kaka.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    ProductService productService;

    @RequestMapping("/findAll")
    public String findAll(@RequestParam(value = "page", defaultValue = "1", required = false) Integer page,
                          @RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit,
                          @RequestParam(value = "productName", defaultValue = "", required = false) String productName,
                          HttpServletRequest request) {
        PageHelper.startPage(page, limit);
        List<Product> productList = productService.selectAll(productName);
        PageInfo<Product> pageInfo = new PageInfo<>(productList);
        request.setAttribute("pageInfo", pageInfo);
        //将查询的关键字回显 在分页中使用
        request.setAttribute("value", productName);
        return "/product/product-list.jsp";
    }

    @RequestMapping("/show")
    public String show(int id, HttpServletRequest request) {
        Product product = productService.selectById(id);
        request.setAttribute("product", product);
        return "/product/product-show.jsp";
    }

    @RequestMapping("/findById")
    public String findById(int id, HttpServletRequest request) {
        Product product = productService.selectById(id);
        request.setAttribute("product", product);
        return "/product/product-edit.jsp";
    }

    @GetMapping("/edit")
    public String edit(Product product) {
        productService.updateProduct(product);
        return "redirect:findAll";
    }

    @GetMapping("/add")
    public String add(Product product) {
        System.out.println(product);
        productService.insertProduct(product);
        return "redirect:findAll";
    }

    @GetMapping("/deleteById")
    public String deleteById(Integer id) {
        productService.deleteProductById(id);
        return "redirect:findAll";
    }
    /*====================================================================*/

    @RequestMapping("/findAllJson")
    @ResponseBody
    public PageInfo<Product> findAllJson(@RequestParam(value = "page", defaultValue = "1", required = false) Integer page,
                                         @RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit,
                                         @RequestParam(value = "productName", defaultValue = "", required = false) String productName,
                                         HttpServletRequest request) {
        PageHelper.startPage(page, limit);
        List<Product> productList = productService.selectAll(productName);
        PageInfo<Product> pageInfo = new PageInfo<>(productList);
        request.setAttribute("pageInfo", pageInfo);
        //System.out.println(pageInfo);
        return pageInfo;
    }

    @PutMapping("/editJson")
    public String editJson(@RequestBody Product product) {
        productService.updateProduct(product);
        return "redirect:findAllJson";
    }

    @PostMapping("/addProductJson")
    public String addProductJson(@RequestBody Product product) {
        System.out.println(product);
        productService.insertProduct(product);
        return "redirect:findAllJson";
    }

    @DeleteMapping("/deleteProductJson")
    public String deleteProductJson(Integer id) {
        productService.deleteProductById(id);
        return "redirect:findAllJson";
    }

    @DeleteMapping("/deleteProductsJson")
    public String deleteProductsJson(@RequestParam(value = "ids",required = false) Object ids) {
        System.out.println("==============");
        System.out.println(ids);
        Integer[] ids1 = (Integer[]) ids;
        System.out.println(Arrays.toString(ids1));
        //productService.deleteProductByIds(ids);
        //return "redirect:findAllJson";
        return "redirect:findAllJson";
    }
}